<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入昵称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择状态">
            <el-option
              v-for="item in dict.type.daily_work_report_status"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="dateRange">
          <el-date-picker
            v-model="queryParams.dateRange"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <TableCard>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['cooperate:dayReport:add']"
            >新增
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

          <el-table :border="Global.tableShowBorder" v-loading="loading" :data="list">
            <el-table-column label="用户名" align="center" prop="userName"/>
            <el-table-column label="昵称" align="center" prop="nickName"/>
            <el-table-column label="部门名称" align="center" prop="deptName"/>
            <el-table-column label="汇报日" align="center" prop="reportDate"/>
            <!-- <el-table-column label="汇报内容" align="center" prop="content">
              <template slot-scope="{row}">
                <span v-html="row.content"></span>
              </template>
            </el-table-column> -->
            <el-table-column label="是否点赞" align="center" prop="like">
              <template slot-scope="{row}">
                <dict-tag :options="dict.type.comm_yes_no" :value="row.like"/>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="{row}">
                <dict-tag :options="dict.type.daily_work_report_status" :value="row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
              <template slot-scope="{row}">
                <el-button v-if="row.status == 1"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(row)"
                  v-hasPermi="['cooperate:dayReport:edit']"
                >修改
                </el-button>
                <el-button v-if="row.status == 1"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(row)"
                  v-hasPermi="['cooperate:dayReport:remove']"
                >删除
                </el-button>
                <el-button
                type="text"
                @click="handleDetail(row)"
                v-hasPermi="['cooperate:dayReport:query']">查看详情</el-button>
                <el-button
                type="text"
                @click="handledianzan(row)"
                v-hasPermi="[row.like == 1 ? 'cooperate:reportLike:edit' : 'cooperate:reportLike:add']">{{row.like == 1 ? '取消点赞' : '点赞'}}</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
      </TableCard>
      <!-- 对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row class="diy-text-sm diy-leading-10" :gutter="10" v-if="title == '查看详情'">
            <el-col :span="item.span || 12" v-for="(item,index) in formDetail" :key="index">
              <div class="flex_hcneter">
                <svg-icon v-if="item.prop != 'attachmentList'" icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
                <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                  {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
                </div>
                <div v-else-if="item.prop == 'content' && item.isCustom">
                  {{item.label}}：
                  <div v-html="form[item.prop]"></div>
                </div>
                <div v-else-if="item.prop == 'images' && item.isCustom">
                  <h2>{{item.label}}</h2>
                  <el-image  v-for="(item1,index1) in form[item.prop]" :key="index1"
                    style="width: 100px; height: 100px;margin-right:10px;margin-bottom:15px;"
                    :src="item1"
                    :preview-src-list="[item1]">
                  </el-image>
                </div>
                <div v-else-if="item.prop == 'attachmentList' && item.isCustom">
                  <h2>{{item.label}}</h2>
                  <el-row v-for="(item1,index1) in form[item.prop]" :key="index1">
                    <el-col :span="24">
                      <svg-icon  icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
                      文件名称：{{item1.fileName}}
                    </el-col>
                    <el-col :span="24">
                      <div class="flex_hcneter">
                        <svg-icon  icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
                        文件路径：<el-link type="primary" @click="urlLink(item1.url)">{{item1.url}}</el-link>
                      </div>
                    </el-col>
                  </el-row>

                </div>
                <div v-else-if="item.prop == 'comments' && item.isCustom" class="flex_flex" style="margin-bottom:15px;">
                  <div class="flex_hcneter_bet">
                    <h2>{{item.label}}</h2>
                    <el-button
                      type="primary"
                      plain
                      icon="el-icon-plus"
                      size="mini"
                      @click="handleplAdd"
                      v-hasPermi="['cooperate:reportComment:add']"
                    >新增
                    </el-button>
                  </div>
                  <el-table :border="Global.tableShowBorder" :data="form[item.prop]">
                    <el-table-column label="用户名" align="center" prop="userName"/>
                    <el-table-column label="昵称" align="center" prop="nickName"/>
                    <el-table-column label="用户头像" align="center" prop="avatar">
                      <template slot-scope="{row}">
                        <el-image
                          style="width: 100px; height: 100px"
                          :src="row.avatar"
                          :preview-src-list="[row.avatar]">
                        </el-image>
                      </template>
                    </el-table-column>
                    <el-table-column label="评论内容" align="center" prop="content" show-overflow-tooltip>
                      <template slot-scope="{row}">
                        <span v-html="row.content"></span>
                      </template>
                    </el-table-column>
                    <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                      <template slot-scope="{row}">
                        <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-edit"
                          @click="handleplUpdate(row)"
                          v-hasPermi="['cooperate:reportComment:edit']"
                        >修改
                        </el-button>
                        <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-delete"
                          @click="handleplDelete(row)"
                          v-hasPermi="['cooperate:reportComment:remove']"
                        >删除
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
                <div v-else-if="item.prop == 'signInList' && item.isCustom" class="flex_flex">
                  <el-table :border="Global.tableShowBorder" :data="form[item.prop]">
                    <el-table-column label="签到时间" align="center" prop="signInTime"/>
                    <el-table-column label="位置名称" align="center" prop="location" />
                    <el-table-column label="签到图片" align="center" prop="photos">
                      <template slot-scope="{row}">
                        <el-link type="primary" style="font-size:16px;" @click="qiandao(row)">签到图片</el-link>
                      </template>
                    </el-table-column>
                    <el-table-column label="备注" align="center" prop="note" show-overflow-tooltip/>
                  </el-table>
                </div>
                <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
              </div>
            </el-col>
          </el-row>
          <div v-else>
            <el-row>
              <el-col :span="24" v-if="!form.id">
                <el-form-item label="汇报日" prop="reportDate">
                  <el-date-picker
                    v-model="form.reportDate"
                    type="date" value-format="yyyy-MM-dd"
                    placeholder="选择汇报日">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="汇报内容" prop="content">
                  <el-input v-model="form.content" type="textarea" :rows="6" placeholder="请输入汇报内容"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="图片" prop="images">
                  <imageUpload
                    :fileType="['bmp','jpg','png','gif',]" returnData="Array" fileName="files" :fileData="{'bizType':'dayReport',}"
                    action="/oa-pcapi/cooperate/file/uploadAtt"
                    v-model="form.images" :limit="100"></imageUpload>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="状态" prop="status">
                  <el-radio-group v-model="form.status">
                    <el-radio  v-for="item in dict.type.daily_work_report_status"
                    :key="item.value"
                    :label="item.value">{{item.label}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item  label="附件" prop="attachment"><el-button type="primary" icon="el-icon-plus" @click="addList('attachment')"></el-button></el-form-item>
            <el-row v-for="(item,index) in form.attachment" :key="index">
              <el-col :span="24">
                <!--
                :rules="{
                  required: true, message: '文件名称不能为空', trigger: 'blur'
                }" -->
                <el-form-item label="文件名称"
                :prop="'attachment.' + index + '.fileName'"
                >
                  <el-input v-model="item.fileName"  placeholder="请输入文件名称"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <!--
                :rules="{
                  required: true, message: '文件路径不能为空', trigger: 'blur'
                }" -->
                <el-form-item label="文件路径"
                :prop="'attachment.' + index + '.url'"
                >
                  <div class="flex_hcneter">
                    <FileUpload :limit="1"  v-model="item.url"
                    :fileType="null" returnData="Array"
                    fileName="files" :fileData="{'bizType':'dayReport',}"
                    action="/oa-pcapi/cooperate/file/uploadAtt" btnName="选择"></FileUpload>
                    <el-button style="margin-left:10px;" v-if="form.attachment.length > 1"
                    @click="delList(index,'attachment')" type="primary" icon="el-icon-minus"></el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </div>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" v-if="title != '查看详情'" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 对话框 -->
      <el-dialog :title="plTitle" :visible.sync="plOpen" width="50%" append-to-body>
        <el-form ref="plForm" :model="plForm" :rules="plRules" label-width="120px">
          <el-form-item label="评论内容" prop="content">
            <el-input v-model="plForm.content" type="textarea" :rows="6" placeholder="请输入评论内容"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="plSubmitForm">确 定</el-button>
          <el-button @click="plOpen = false">取 消</el-button>
        </div>
      </el-dialog>

      <el-image-viewer
        v-if="showViewer"
        :zIndex='9999'
        :url-list="srcList"
        :initialIndex="initialIndex"
        :on-close="closeViewer">
      </el-image-viewer>
  </div>
</template>

<script>
import {listDayReport, getDayReport, addDayReport, updateDayReport, delDayReport} from "@/api/collaWorryFree/dailyWorkReport";
import {getReportComment, addReportComment, updateReportComment, delReportComment,like,unlike,} from "@/api/collaWorryFree/comment";
export default {
  name: "DailyWorkReport",
  dicts: ['daily_work_report_status','comm_yes_no',],
  data() {
    return {
      showViewer: false,//签到图片显示
      initialIndex:0,//签到图片下标
      srcList:[],//签到图片
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      plTitle: "",
      // 是否显示弹出层
      open: false,
      plOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateRange: [],userName:null,status:null,nickName:null,
      },
      // 表单参数
      form: {},
      plForm: {},
      // 表单校验
      rules: {
        reportDate: [ {required: true, message: "汇报日不能为空", trigger: "change"} ],
        content: [ {required: true, message: "汇报内容不能为空", trigger: "blur"} ],
        status: [ {required: true, message: "状态不能为空", trigger: "change"} ],
      },
      plRules: {
        content: [ {required: true, message: "汇报内容不能为空", trigger: "blur"} ],
      },
      formDetail:[
        {label:'用户名',prop:'userName',},{label:'昵称',prop:'nickName',},{label:'部门名称',prop:'deptName',},{label:'汇报日',prop:'reportDate',},
        {label:'汇报内容',prop:'content',isCustom:true,span:24,},{label:'状态',prop:'status',zidian:'daily_work_report_status',isCustom:true,},
        {label:'是否已读',prop:'read',zidian:'comm_yes_no',isCustom:true,},{label:'是否点赞',prop:'like',zidian:'comm_yes_no',isCustom:true,},
        {label:'已读数量',prop:'readNum',}, {label:'点赞数量',prop:'likeNum',}, {label:'评论数量',prop:'commentNum',},{label:'签到数量',prop:'signInNum',},
        {label:'创建时间',prop:'createTime',},{label:'图片列表',prop:'images',isCustom:true,span:24,},
        {label:'附件列表',prop:'attachmentList',isCustom:true,span:24,},{label:'评论列表',prop:'comments',isCustom:true,span:24,},
        {label:'签到列表',prop:'signInList',isCustom:true,span:24,},

      ],
    };
  },
  created() {
      this.getList();
  },
  methods: {
    qiandao(e){
      let photos = e.photos || ''
      if(!photos) return this.$modal.msgError('暂无签到图片');
      let arr = photos.split(',')
      this.srcList = arr
      this.showViewer = true;
    },
    // 敞开查看器
    closeViewer() {
      this.showViewer = false;
    },
    urlLink(row){//链接跳转
      window.open(row)
    },
    addList(ziduan){//添加列表
      this.form[ziduan].push({})
    },
    delList(index,ziduan){//删除列表
      this.form[ziduan].splice(index,1)
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      let dateRange = queryParams.dateRange || []
      if(dateRange.length) {
        queryParams.reportDateStart = dateRange[0]
        queryParams.reportDateEnd = dateRange[1]
      }
      delete queryParams.dateRange
      listDayReport(queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {status:'1',attachment:[{}],};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加日工作汇报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDayReport(id).then(response => {
        let data = response.data || {};
        this.form = {
          ...data,
          attachment:data.attachmentList || [{}],
        }
        this.open = true;
        this.title = "修改日工作汇报";
      });
    },
    handleDetail(row) {//查看详情
      const id = row.id
      this.open = true;
      this.title = "查看详情";
      this.getDayReport(id)
    },
    async getDayReport(id){
      let response = await getDayReport(id)
      let data = response.data || {}
      let images = data.images || ''
      let comments = data.comments || []
      this.form = {
        ...data,attachment:data.attachmentList || [],
        content:this.$changeLine(data.content || ''),
        comments:comments.map(item=>{
          return {...item,content:this.$changeLine(item.content || ''),}
        }),images:images ? images.split(',') : []
      }
      console.log('this.form',this.form)
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateDayReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDayReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 表单重置
    plReset() {
      this.plForm = {};
      this.resetForm("plForm");
    },
    /** 新增按钮操作 */
    handleplAdd() {
      this.plReset();
      this.plForm.reportId = this.form.id
      this.plOpen = true;
      this.plTitle = "添加评论";
    },
    handleplUpdate(row) {
      this.plReset();
      const id = row.id
      getReportComment(id).then(response => {
        let data = response.data || {};
        this.plForm = { ...data, }
        this.plOpen = true;
        this.plTitle = "修改评论";
      });
    },
    plSubmitForm: function () {
      this.$refs["plForm"].validate(valid => {
        if (valid) {
          let plForm = {...this.plForm}
          if (this.plForm.id != undefined) {
            updateReportComment(plForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.plOpen = false;
              this.getDayReport(this.form.id);
            });
          } else {
            plForm.category = 1
            addReportComment(plForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.plOpen = false;
              this.getDayReport(this.form.id);
            });
          }
        }
      });
    },
    handledianzan(e){//点赞和取消点赞
      let api = e.like == 1 ? unlike : like
      let title = e.like == 1 ? '取消点赞' : '点赞'
      this.$modal.confirm(`是否确认${title}该数据项？`).then(function () {
        return api({category:1,reportId:e.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(title + "成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleplDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
        return delReportComment(id);
      }).then(() => {
        this.getDayReport(this.form.id);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
          return delDayReport(id);
      }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
